package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.StartBuildAttDao;
import top.infopub.mgr.ic.dao.StartBuildDao;
import top.infopub.mgr.ic.dao.StartBuildRiskDao;
import top.infopub.mgr.ic.domain.StartBuildDomain;
import top.infopub.mgr.ic.service.StartBuildService;
import top.infopub.mgr.ic.vo.StartBuildVo;
import top.infopub.model.ic.StartBuild;
import top.infopub.model.ic.StartBuildAtt;
import top.infopub.model.ic.StartBuildRisk;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class StartBuildServiceImpl extends GenericServiceImpl<StartBuild, String> implements
		StartBuildService {

	@Autowired
	private StartBuildDao startBuildDao;
	
	@Autowired
    private StartBuildAttDao startBuildAttDao;
    
	@Autowired
	private StartBuildRiskDao startBuildRiskDao;
	
    @Autowired
    private WsFlowProcessService wsFlowProcess;
    
	
	@Override
	public GenericDao<StartBuild, String> getDao() {
		return startBuildDao;
	}

    @Override
    public Page<StartBuildDomain> selectByDomainWhere(Page<StartBuildDomain> page,
                                                      StartBuildVo startBuildVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<StartBuildDomain>)startBuildDao.selectByDomainWhere(startBuildVo);
    }

    @Override
    public Page<StartBuildDomain> selectMyCheckByDomainWhere(Page<StartBuildDomain> page,
                                                             StartBuildVo startBuildVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<StartBuildDomain>)startBuildDao.selectMyCheckByDomainWhere(startBuildVo);
    }

    @Override
    public StartBuildVo queryById(String id) {
        return startBuildDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStartBuild(StartBuildVo startBuild, List<StartBuildRisk> startBuildRisks,
                               List<StartBuildAtt> startBuildAtts) {
        startBuildDao.insertById(startBuild);
        
        if(CollectionUtils.isNotEmpty(startBuildRisks)){
            startBuildRiskDao.insertByBatch(startBuildRisks);
        }
        
        if(CollectionUtils.isNotEmpty(startBuildAtts)){
            startBuildAttDao.insertByBatch(startBuildAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_START_BUILD;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, startBuild.getId(), startBuild.getProcessId(), null);
        
    }

}